package caidan.net;

import caidan.MainApp;
import caidan.SetFoodDataDialog;
import caidan.data.CategoryData;
import caidan.res.Resource;
import jinyilw.swing.SystemTools;
import jinyilw.swing.image.ImageTools;
import jinyilw.common.TextTools;
import jinyilw.common.app.AppConfig;
import jinyilw.common.file.FileTools;
import jinyilw.swing.ChooseFileUtils;
import jinyilw.swing.FontTools;
import jinyilw.swing.SwingTools;
import jinyilw.swing.visuals.Rcomp.RDialog;
import jinyilw.swing.visuals.Rcomp.RFileChooser;
import jinyilw.swing.visuals.Rcomp.RTabbedPane;
import jinyilw.swing.visuals.comp.ImageComp;
import jinyilw.swing.visuals.comp.ImageTextComp;
import jinyilw.swing.visuals.window.HintWindow;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class NewFoodView extends RDialog
{
	private static final long serialVersionUID = -8373297123667603138L;

	private final AbstractNewFoodPanel newFoodPanel;
	private RTabbedPane tabbedPane;
	private JPanel imagePanel, filePanel;
	private JPanel imageBottom, fileBottom;
	private ImageComp imageComp;
	private ImageTextComp fileNameComp;
	private ImageComp fileImageComp;
	private JTextArea textArea;
	private JScrollPane textScrollPane;
	private boolean isText = true;
	private JButton setButton, saveButton, addButton;
	private JButton textActButton;
	private JPopupMenu addPopupMenu;
	private int popupMenuHeight;
	private NewFood newFood;

	public NewFoodView(Window w, AbstractNewFoodPanel newFoodPanel)
	{
		super(w);
		this.newFoodPanel = newFoodPanel;
		setSize(550, 550);
		setTitleHeight(40);
		setTitleFont(FontTools.getCustomFont(18));
		setLocationRelativeTo(null);
		init();
	}

	public void setNewFood(NewFood nFood)
	{
		if (newFood == nFood)
			return;
		newFood = nFood;
		setTitle("〖" + nFood.getUser() + "〗推荐: " + nFood.getFoodName());
		imageComp.setImage(nFood.getImage());
		File makeFile = nFood.getMakeFile();
		if (makeFile == null)
			return;
		boolean isImage = ImageTools.isNormalImage(makeFile.getName());
		if (isText != nFood.isText())
		{
			isText = nFood.isText();
			if (isText)
			{
				filePanel.remove(fileNameComp);
				filePanel.remove(fileImageComp);
				filePanel.add(textScrollPane, BorderLayout.CENTER);
				textActButton.setText("复制文本");
			} else
			{
				filePanel.remove(textScrollPane);
				if (isImage)
					filePanel.add(fileImageComp, BorderLayout.CENTER);
				else
					filePanel.add(fileNameComp, BorderLayout.CENTER);
				textActButton.setText("打开文件");
			}
			filePanel.revalidate();
		} else if (!isText)
		{
			if (isImage)
			{
				filePanel.remove(fileNameComp);
				filePanel.add(fileImageComp, BorderLayout.CENTER);
			} else
			{
				filePanel.remove(fileImageComp);
				filePanel.add(fileNameComp, BorderLayout.CENTER);
			}
			filePanel.revalidate();
		}

		if (isText)
			textArea.setText(nFood.getText());
		else if (isImage)
			fileImageComp.setImage(makeFile);
		else
			fileNameComp.setText(makeFile.getAbsolutePath());

		filePanel.repaint();
	}

	//	public void updateAddPopMenu()
	//	{
	//		addPopupMenu = null;
	//	}

	private void init()
	{
		createGUI();
		initLayout();
		initListeners();
	}

	private void createGUI()
	{
		tabbedPane = new RTabbedPane();
		imagePanel = tabbedPane.createTabbedPanel("图片预览");
		filePanel = tabbedPane.createTabbedPanel("制作方法");
		imageBottom = new JPanel();
		fileBottom = new JPanel();
		imageComp = new ImageComp();
		fileNameComp = new ImageTextComp();
		fileNameComp.setImageLocation(SwingConstants.NORTH);
		fileNameComp.setGap(20);
		fileNameComp.setImage(Resource.bigFileIcon);
		fileImageComp = new ImageComp();
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setFont(FontTools.defaultFont);
		textScrollPane = new JScrollPane(textArea);
		setButton = new JButton("设置菜单");
		saveButton = new JButton("保存图片");
		addButton = new JButton("添加到菜单");
		textActButton = new JButton("复制文本");
	}

	private void initLayout()
	{
		add(tabbedPane);
		imagePanel.setLayout(new BorderLayout());
		imagePanel.add(imageComp, BorderLayout.CENTER);
		imagePanel.add(imageBottom, BorderLayout.SOUTH);
		imageBottom.add(setButton);
		imageBottom.add(saveButton);
		imageBottom.add(addButton);
		filePanel.setLayout(new BorderLayout());
		filePanel.add(textScrollPane, BorderLayout.CENTER);
		filePanel.add(fileBottom, BorderLayout.SOUTH);
		fileBottom.add(textActButton);
	}

	private RFileChooser saveFileChooser;
	private void initListeners()
	{
		setButton.addActionListener((ActionEvent e) ->
		{
			dispose();
			SetFoodDataDialog setFoodDataDialog = newFoodPanel
					.getSetFoodDataDialog();
			setFoodDataDialog.updateContent(newFood.getImage(),
					newFood.getFoodName(), newFood.getDataFile());
			setFoodDataDialog.setVisible(true);
			newFood.setFoodName(setFoodDataDialog.getSettingName());
		});
		saveButton.addActionListener((ActionEvent e) ->
		{
			if (saveFileChooser == null)
				saveFileChooser = ChooseFileUtils.getDirectoryChooser(
						AppConfig.getAppDataFolder(), "选择保存目录");
			File forder = saveFileChooser.getSaveDirectory(this);
			if (forder == null)
				return;
			String ext = TextTools.getFileExt(newFood.getImageFile().getPath());
			File saveFile = new File(forder, newFood.getFoodName() + "." + ext);
			if (!RFileChooser.isOverride(saveFile, this))
				return;
			FileTools.copyFile(newFood.getImageFile(), saveFile);
			HintWindow.showHint("保存成功", this);
		});
		addButton.addActionListener((ActionEvent e) ->
		{
			if (addPopupMenu == null)
			{
				addPopupMenu = createAddPopMenu();
				popupMenuHeight = 0 - addPopupMenu.getPreferredSize().height;
			}
			addPopupMenu.show(addButton, 0, popupMenuHeight);
		});
		textActButton.addActionListener((ActionEvent e) ->
		{
			if (isText)
			{
				SystemTools.setSysClipboardText(textArea.getText());
				HintWindow.showHint("复制成功", this);
			} else
			{
				SystemTools.openFile(newFood.getMakeFile());
			}
		});

		for (CategoryData categoryData : MainApp.getCategoryDatas())
			categoryData.addNameChangedOperater((String name) ->
			{
				addPopupMenu = null;
			});
	}

	private JPopupMenu createAddPopMenu()
	{
		CategoryData[] categoryDatas = MainApp.getCategoryDatas();
		String[] strings = new String[categoryDatas.length * 2];
		ActionListener[] listeners = new ActionListener[categoryDatas.length
				* 2];
		int i = 0;
		for (CategoryData categoryData : categoryDatas)
		{
			strings[i] = "添加到" + categoryData.getCategoryName() + "(早餐)";
			listeners[i] = (ActionEvent e) ->
			{
				boolean succeed = categoryData.addNew(newFood.getImageFile(),
						newFood.getFoodName(), true, this);
				if (succeed)
					HintWindow.showHint("添加成功", this);
			};
			i++;
			strings[i] = "添加到" + categoryData.getCategoryName() + "(午餐、晚餐)";
			listeners[i] = (ActionEvent e) ->
			{
				boolean succeed = categoryData.addNew(newFood.getImageFile(),
						newFood.getFoodName(), false, this);
				if (succeed)
					HintWindow.showHint("添加成功", this);
			};
			i++;
		}
		return SwingTools.createPopMenu(strings, listeners);
	}
}
